package com.example.pc.myapplication.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.pc.myapplication.R;
import com.example.pc.myapplication.pojo.HotData;
import com.example.pc.myapplication.viewholder.Type1ViewHolder;
import com.example.pc.myapplication.viewholder.UnTypeViewHolder;

public class HotAdapter extends RecyclerView.Adapter{

    private Context context;
    private HotData hotData;

    public HotAdapter(Context context) {
        this.context = context;
    }

    public HotData getHotData() {
        return hotData;

    }
    private static final int TYPE_1 = 1;
//    private static final int TYPE_2 = 2;
//    private static final int TYPE_24 = 24;
//    private static final int TYPE_10 = 10;
//    private static final int TYPE_11 = 11;
//    private static final int TYPE_15 = 15;
//    private static final int TYPE_4 = 4;
//    private static final int TYPE_23 = 23;
//    private static final int TYPE_20 = 20;
//    private static final int TYPE_13 = 13;

        /**hotData关联适配器数据*/
    public void setHotData(HotData hotData) {
        this.hotData = hotData;
        notifyDataSetChanged();//刷新适配器
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取布局加载器
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        switch (viewType){
            case TYPE_1:
                return new Type1ViewHolder(context, layoutInflater.inflate(R.layout.view_rv_item_type_1, parent, false));
        }

//        return new UnTypeViewHolder(layoutInflater.inflate(R.layout.view_rv_item_untype, parent, false));
        return new UnTypeViewHolder(layoutInflater.inflate(R.layout.view_rv_item_type_1, parent, false));
    }

    @Override//此方法是对RecyclerView子项进行赋值的,屏幕滚动的时候会被执行
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        HotData.ListBeanX listBeanX = hotData.getList().get(position);
        if (holder instanceof Type1ViewHolder){
            Type1ViewHolder type1ViewHolder = (Type1ViewHolder) holder;
            type1ViewHolder.setData(listBeanX);
        }

    }
    @Override//确定 Item 的ViewHolder 类型
    public int getItemViewType(int position) {
        HotData.ListBeanX listBeanX = hotData.getList().get(position);
        //发现,喜马拉雅的App 是以 每个 Item的 moduleId 决定布局类型
        return listBeanX.getModuleId();
    }

    @Override
    public int getItemCount() {
        return hotData == null ? 0 : hotData.getList().size();
//        return hotData == null ? 0 : 8;
    }
}
